package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.SystemConfigsAdapter;
import com.xianbeiying.app.ckgl.bean.SysConfigBean;
import com.xianbeiying.app.ckgl.bean.SystemConfigBean;
import com.xianbeiying.app.ckgl.bean.SystemDeleteBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.DeleteModel;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.DeviceUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.dapengView.DpView;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;

import java.util.List;

/**
 * 系统配置
 * */
public class SystemConfigActivity extends BaseActivity  implements View.OnClickListener,HttpCallBackUtil{
    //添加作物
    private LinearLayout ly_add_crop;
    private ImageView iv_title_left;
    private TextView tv_title;
    private ListView system_cof_list;
    private GetModel getModel;
    private DeleteModel deleteModel;
    private DpView dpView;
    //水平滑动的
    private LinearLayout ly_wphorizontal;  //水平滑动的布局
    private View va_line;
    private TextView tv_va_all;
    private SystemConfigBean systemConfigBean;
    private List<SysConfigBean> systemConfigBeanArrayList;
//    private SystemConfigAdapter systemConfigAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_config);
        initView();
    }
    private void initView() {
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        deleteModel=new DeleteModel(this);
        deleteModel.addResponseListener(this);
        ly_add_crop = (LinearLayout) findViewById(R.id.ly_add_crop);
        ly_add_crop.setOnClickListener(this);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        system_cof_list = (ListView) findViewById(R.id.system_cof_list);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("系统配置");
        ly_wphorizontal = (LinearLayout) findViewById(R.id.ly_wphorizontal);
        va_line = (View) findViewById(R.id.va_line);
        tv_va_all = (TextView) findViewById(R.id.tv_va_all);
        iv_title_left.setOnClickListener(this);
        tv_va_all.setOnClickListener(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        intDate();
    }
    private void intDate() {
        getModel.GET_CROP_STEP_ALL_INFO("GET_CROP_STEP_ALL_INFO",true);
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            //添加农作物
            case R.id.ly_add_crop:
               startActivity(new Intent().setClass(SystemConfigActivity.this,PlantCropActivity.class));
                break;
            case R.id.iv_title_left:
                finish();
                break;
            //全部
            case R.id.tv_va_all:
                va_line.setVisibility(View.GONE);
                tv_va_all.setVisibility(View.GONE);
                break;
            //删除
            case R.id.tv_delete:
                deleteModel.getDELETE_CROPSTEP("DELETE_CROPSTEP",systemConfigBeanArrayList.get(selectId).getCropId()+"",systemConfigBeanArrayList.get(selectId).getConfigBean().get(0).getCropType()+"",
                        systemConfigBeanArrayList.get(selectId).getPlantType()+"",true);
                break;
            //取消
            case R.id.tv_cancel:
            case R.id.ly_exit:
                centerPopWindow.dismiss();
                break;
        }
    }
    private  SystemConfigsAdapter systemConfigsAdapter;
    private SystemDeleteBean systemDeleteBean;
    @Override
    public void onCallBackData(String url, Object content){
        if(url.equals("生成阶段")){
        systemConfigBean= JSON.parseObject(new String((byte[]) content), SystemConfigBean.class);
        if(systemConfigBean.isR()){
            if(systemConfigBean.getV().getCROPSTEPALLINFOS()!=null&&systemConfigBean.getV().getCROPSTEPALLINFOS().size()!=0){
                systemConfigBeanArrayList= DeviceUtil.getSystemBeanmm(systemConfigBean);
                systemConfigsAdapter=new SystemConfigsAdapter(this,systemConfigBeanArrayList);
                system_cof_list.setAdapter(systemConfigsAdapter);
                systemConfigsAdapter.setHandler(handler);
            }else{
                system_cof_list.setAdapter(null);
            }
        }
        }
        if(url.equals("删除生长周期")){
            systemDeleteBean=JSON.parseObject(new String((byte[]) content), SystemDeleteBean.class);
            if(systemDeleteBean.isR()){
                ToastUtil.showInfo(SystemConfigActivity.this,"删除成功");
                centerPopWindow.dismiss();
                getModel.GET_CROP_STEP_ALL_INFO("GET_CROP_STEP_ALL_INFO",true);
            }else{
                ToastUtil.showInfo(SystemConfigActivity.this,systemDeleteBean.getERR_MSG());
                centerPopWindow.dismiss();
            }
        }
    }
    private int selectId;//删除的哪一条
    private CenterPopWindow centerPopWindow;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //进行删除
            centerPopWindow = new CenterPopWindow(SystemConfigActivity.this, R.layout.pop_window_delete);
            centerPopWindow.setText(R.id.tv_title,"确定删除");
            centerPopWindow.setTextOnClickListener(R.id.tv_delete,SystemConfigActivity.this);
            centerPopWindow.setTextOnClickListener(R.id.tv_cancel, SystemConfigActivity.this);
            centerPopWindow.setTextOnClickListener(R.id.ly_exit, SystemConfigActivity.this);
            selectId=msg.arg1;
        }
    };
}
